package com.linkoog.devtools.json.toolwindow.ui;

import com.intellij.json.JsonLanguage;
import com.intellij.lang.Language;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Splitter;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.ui.GuiUtils;
import com.linkoog.devtools.json.file_editer.editer.ui.JackJsonTreeNode;
import com.linkoog.devtools.json.file_editer.editer.ui.JackJsonTreeUtils;
import com.linkoog.devtools.utils.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

public class JsonContentPanel {

    private Project project;
    private String json;
    private JPanel myMainPanel;
    private JTextField keyTextField;
    private JPanel jsonPanel;
    private JPanel topContextPanel;
    private JPanel buttonContextPanel;
    private JScrollPane jsonTreeScrollPane;

    private FileEditor jsonEditor;

    public JsonContentPanel(@NotNull Project project, String json){
        this.project = project;
        this.json = StringUtils.isBlank(json) ? "" : json;


        topContextPanel.setPreferredSize(new Dimension(-1, 790));
        initJsonPanel();

        GuiUtils.replaceJSplitPaneWithIDEASplitter(myMainPanel);
        Splitter splitter = (Splitter)myMainPanel.getComponent(0);
        splitter.setDividerPositionStrategy(Splitter.DividerPositionStrategy.KEEP_FIRST_SIZE);
        //splitter.setOrientation(true);
        splitter.setProportion(0.745F);
        splitter.setShowDividerControls(true);
        //splitter.setHonorComponentsMinimumSize(true);


        JackJsonTreeNode root = JackJsonTreeUtils.buildJsonTree(json);
        JsonTreeTableView jsonTreeTableView = new JsonTreeTableView(root);
        jsonTreeScrollPane.getViewport().add(jsonTreeTableView);
    }

    private void initJsonPanel() {
        jsonEditor = createEditor(JsonLanguage.INSTANCE.getID(), json);
        jsonPanel.add(jsonEditor.getComponent(), BorderLayout.CENTER);
    }

    /**
     * set content for editor
     */
    private void setEditorDoc(String text) {
        WriteCommandAction.runWriteCommandAction(project, () -> {
            Document doc = getDocumentFromEditor(jsonEditor);
            doc.deleteString(0, doc.getTextLength());
            doc.setText(text == null ? "" : text.replaceAll("\\r", ""));
        });
    }

    private String getStringFromEditor() {
        Document doc = getDocumentFromEditor(jsonEditor);
        return doc.getText();
    }

    private Document getDocumentFromEditor(FileEditor editor) {
        return FileDocumentManager.getInstance().getCachedDocument(editor.getFile());
    }

    private FileEditor createEditor(String languageId, String text) {
        if (StringUtils.isEmpty(text)) {
            text = "";
        }

        Language language = Language.findLanguageByID(languageId);
        PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(language, text);
        FileEditor editor = TextEditorProvider.getInstance().createEditor(project, psiFile.getVirtualFile());
        return editor;
    }

    public JPanel getMyMainPanel() {
        return myMainPanel;
    }
}
